<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\first\model\Notice;
use think\Db;

class Active extends Base {
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $field
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
        $list = $this
            ->field($field,$out)
            ->with(['member','information'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => $v){
            $list[$k]['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $list[$k]['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    public function information(){
        return $this->hasOne('ArticleInformation','aid','id');
    }
    /**
     * 审核
     * @param $id
     * @param int $type
     * @param string $info
     * @param null $out
     * @return bool
     */
    public function check($id,$type=1,$info='',&$out=null){
        $_info = $this->find($id);
        if(!$_info){
            $out = '申请不存在';
            return false;
        }
        if($_info['status']!=1){
            $out = '申请处理了';
            return false;
        }
        if($type==2){
            if(empty($info)){
                $out = '请输入拒绝理由';
                return false;
            }
            $title= "审核失败";
            $_info->info = $info;
            $_status = 3;
            $content = "抱歉！您发布的梦想加{$_info['title']}未通过审核！";
        }else{
            $_status = 2;
            $title= "审核成功";
            $content = "恭喜您！您发布的梦想加{$_info['title']}通过了审核！";
            $end = date('Y-m-d',time() + 60 * 60 * 24 * 30);
            $_info->end_time = strtotime("{$end} 23:59:59");
        }
        $_info->status = $_status;
        $_info->check_time = time();
        Db::startTrans();
        $result = $_info->save();
        $result2 = (new Notice())->addNew($_info['mid'],$title,$content,2,$_info['cid_type'],$_info['id']);
        if($result && $result2) {
            $_data = $_info->toArray();
            $_data['sql'] = $_info->getLastSql();
            $_data['msg']=$content;
            $out = $_data;
            Db::commit();
            return true;
        }else{
            $out = '操作失败';
            Db::rollback();
            return false;
        }
    }

    /**
     * @param int $id
     * @param string $field
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field($field)
            ->find($id);
        return $info;
    }

    public function add($data){
        $this->save($data);
    }
    
    /**
     * @param $where
     * @return float|string
     */
    public function counts($where){
        $list = $this
            ->alias('a')
            ->join('user c','c.id=a.uid')
            ->where($where)
            ->count('1');
        return $list;
    }

    /*
    public function export($where=[],$order='',&$header=[]){
        $order = $order?$order:'a.id desc';
        $field = ['a.*','b.title as title1','c.account','c.nickname'];
        $list = $this
            ->alias('a')
            ->field($field)
            ->join('user c','c.id=a.uid')
            ->order($order)
            ->where($where)
            ->select();
        $header = [
            'sort'=>'序号',
            'id'=>'ID',
            'type'=>'类型',
            'order_code'=>'订单号',
            'title1'=>'购买名目',
            'account'=>'购买账号',
            'money'=>'总金额',
            'pay_money'=>'支付金额',
            'discount_money'=>'优惠金额',
            'pay_time'=>'支付时间',
            'create_time'=>'下单时间',
            'order_type'=> '下单方式'
        ];
        $total = count($list->toArray());
        $status = ['--','待支付','已支付','无效','--'];
        $order_type = ['','线上交易','线下交易','--'];
        $type = ['--','直播','点播','课程','--'];
        foreach ($list as $k => &$v){
            $v['sort'] = $total - $k;
            $v['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
            $v['status'] = $status[$v['status']];
            $v['order_type'] = $order_type[$v['order_type']];
            $v['type'] = $type[$v['type']];
        }
        $list = $list->toArray();
        return $list;
    }
     */

    public function member(){
        return $this->hasOne('Member','id','mid')
            ->bind([
                'account',
                'nickname'
            ]);
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getImageAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlsById($value);
    }

    /**
     * @param $value
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCoverAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlsById($value);
    }

    /**
     * @param $value
     * @return string
     */
    public function getContentAttr($value){
        if(empty($value)){
            return '';
        }
        return htmlspecialchars_decode($value);
    }

    /**
     * @param $value
     * @return string
     */
    public function setContentAttr($value){
        if(empty($value)){
            return '';
        }
        return htmlspecialchars($value);
    }
}